A MySQL database
needs to provide access to many users at
once and each connection must be secure and
stable so that only authenticated
users can gain access and there's no risk of
their connections failing. But managing secure
and stable connections requires many
resource-heavy actions. So how can you perform
these actions efficiently? The answer lies in database
connection pooling. In this video, you will explore the concept of database
connection pooling, investigate how it works and learn about its advantages. Little Lemon's website has a Python-based application that lets guests book time
slots with the restaurant. The application sends
the guests bookings as SQL statements to the
Little Lemon database. However, the website needs
to provide each guest with a secure and a
stable connection to the Little Lemon database. This is so that they can input their booking data without any risk of connection failure. Little Lemon can manage
these connections with the use of database
connection pooling. Let's find out more. Database connection
pooling involves creating and managing a pool of
connections to run faster, more efficient, and
optimized connections between clients and
a MySQL database. To gain a better understanding of how connection pooling works, let's explore a visual example. Visualize a pool of four open connections
to a MySQL database. Two of the connections
are currently being used by clients to
access the database. The other two connections are free and ready to
use by any new user. A new user can arrive and
request access to the database. The connection pool then assigns this new user one of
the open connections. Shortly after the new user is assigned to their connection, the first two users complete their tasks and their
sessions and leave the pool. Even though the users
have left the pool, the connections remain open. Technically speaking, the
connections aren't closed. They're just placed back in the connection pool where they remain available for new users. There are now three
free and ready-to-use connections that can be
assigned to new users, but what if all four
connections are in use and a fifth
user wants to join? There are only four
connections available. So how can you serve the
fifth user's access request? To avoid this situation, the best approach is to
create multiple pools with a specific number of connections
assigned to each pool. This means that different users can be assigned to
different pools, so there's always an
available connection in at least one
pool for new users. You can also program the system to create
a new connection within a pool if an
appropriate connection isn't available in other pools. Little Lemon can use
this approach to manage connections to their
MySQL database. They can create a series
of connection pools that their guests can use to record their bookings
in the database. There are a few key advantages
to connection pooling. Connection pooling makes efficient use of
available resources. It reduces the time and effort required to establish
connections. Connection pools simplify programming models
and they increase the performance of the
Python application when connecting to
the MySQL database. You should now be familiar with the concept of database
connection pooling, be able to explain how it works and to describe its advantages. There's lots more to learn about connection pooling
in this lesson, but you're off to a great start.